package com.example.controller;

import com.example.anno.Log;
import com.example.pojo.PageResult;
import com.example.pojo.Result;
import com.example.pojo.Student;
import com.example.pojo.StudentQueryParam;
import com.example.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/students")
@RestController
public class StudentController {
    @Autowired
    private StudentService studentService;

    /**
     * 条件分页查询学员信息
     */
    @GetMapping
    public Result page(StudentQueryParam studentQueryParam) {
        log.info("分页查询学员信息，参数：{}", studentQueryParam);
        PageResult<Student> list = studentService.page(studentQueryParam);

        return Result.success(list);
    }


    /**
     * 根据ID批量删除学员
     */
    @Log
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids) {
        log.info("根据ID批量删除学员：{}", ids);
        studentService.deleteByIds(ids);

        return Result.success();
    }

    /**
     * 新增学员
     */
    @Log
    @PostMapping
    public Result add(@RequestBody Student student) {
        log.info("新增学员，参数：{}", student);
        studentService.add(student);

        return Result.success();
    }

    /**
     * 根据ID查询学员信息
     */
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable Integer id) {
        log.info("根据ID查询学员信息，参数：{}", id);
        Student student = studentService.getById(id);

        return Result.success(student);
    }

    /**
     * 修改学员信息
     */
    @Log
    @PutMapping
    public Result update(@RequestBody Student student) {
        log.info("修改学员信息{}", student);
        studentService.update(student);

        return Result.success();
    }

    /**
     * 违纪处理
     */
    @Log
    @PutMapping("/violation/{id}/{score}")
    public Result violation(@PathVariable Integer id, @PathVariable Short score) {
        log.info("违纪处理，学员ID：{}，违纪分数：{}", id, score);
        studentService.violation(id, score);

        return Result.success();
    }
}
